package com.example.rocketmq.service.impl;


import com.example.rocketmq.service.OrderFeignService;
import com.example.rocketmq.service.ProductFeignService;
import com.situ.model.Order;
import com.situ.model.OrderItem;
import com.situ.utils.JsonResult;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@RocketMQMessageListener(topic= "skymall-orders-check",consumerGroup="skymall-order-check-consumer-group")
@Service
public class OrderCheckConsumer implements RocketMQListener<Integer> {
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private OrderFeignService orderFeignService;
    @Autowired
    public void setOrderFeignService(OrderFeignService orderFeignService) {
        this.orderFeignService = orderFeignService;
    }
    private ProductFeignService productFeignService;
    @Autowired
    public void setProductFeignService(ProductFeignService productFeignService) {
        this.productFeignService = productFeignService;
    }

    @Override
    public void onMessage(Integer orderId) {
        //从数据库查询订单
        ResponseEntity<JsonResult<Order>> re=this.orderFeignService.findById(orderId);
        JsonResult< Order>jr=re.getBody();
        if(jr!=null&&jr.isSuccess()){
            Order order= jr.getData();
            if(order.getOrderStatus().equals(0)){//超时未支付


                ResponseEntity <JsonResult<?>> re2=this.orderFeignService.updateOrderStatusTo4(orderId);
                JsonResult<?> jr2=re2.getBody();
                if(jr2!=null&&jr2.isSuccess()){
                    //回滚redis
                    List<OrderItem> orderItems= Objects.requireNonNull(this.orderFeignService.findById(orderId).getBody()).getData().getOrderItems();
                    if(!orderItems.isEmpty()){
                        Integer productId=orderItems.getFirst().getProductId();
                        //redis 库存回滚
                        this.redisTemplate.opsForValue().increment("seckill-stock"+productId);
                        //数据库回滚
                        this.productFeignService.update(productId);
                    }
                }
            }
        }
    }
    //查询所有订单项
    private List<OrderItem> findOrderItemsByOrderId(Integer orderId) {
        ResponseEntity<JsonResult<List<OrderItem>>> re = this.orderFeignService.findOrderItemsByOrderId(orderId);
        JsonResult<List<OrderItem>> jr = re.getBody();
        if (jr != null && jr.isSuccess()) {
            return jr.getData();
        } else {
            return List.of();
        }
    }
}
